package cn.cocho.android.mc.comp.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import cn.cocho.android.mc.R;

public class McDrawableTextView extends TextView {

	private int textTouchColor;
	private int textColor;
	private Drawable pressedDrawable;
	private Drawable normalDrawable;
	private DrawableLocation drawableLocation = DrawableLocation.LEFT;

	private enum DrawableLocation {
		LEFT, TOP, RIGHT, BOTTOM
	}

	public McDrawableTextView(Context context) {
		super(context);
	}

	public McDrawableTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public McDrawableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initTextColor(context, attrs);
		initDrawable(context, attrs);
	}

	private void initTextColor(Context context, AttributeSet attrs) {
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McDrawableTextView);
		textTouchColor = tdarr.getColor(R.styleable.McDrawableTextView_drawableTextView_touchColor, Color.WHITE);
		textColor = getCurrentTextColor();
		tdarr.recycle();
	}

	private void initDrawable(Context context, AttributeSet attrs) {
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McDrawableTextView);

		Drawable normalDrawableTopImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_drawableTop);
		Drawable normalDrawableBottomImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_drawableBottom);
		Drawable normalDrawableLeftImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_drawableLeft);
		Drawable normalDrawableRightImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_drawableRight);

		Drawable pressedDrawableTopImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_pressedDrawableTop);
		Drawable pressedDrawableBottomImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_pressedDrawableBottom);
		Drawable pressedDrawableLeftImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_pressedDrawableLeft);
		Drawable pressedDrawableRightImg = tdarr.getDrawable(R.styleable.McDrawableTextView_drawableTextView_pressedDrawableRight);

		if (normalDrawableBottomImg != null && pressedDrawableBottomImg != null) {
			drawableLocation = DrawableLocation.BOTTOM;
			normalDrawable = normalDrawableBottomImg;
			pressedDrawable = pressedDrawableBottomImg;
		}
		if (normalDrawableTopImg != null && pressedDrawableTopImg != null) {
			drawableLocation = DrawableLocation.TOP;
			normalDrawable = normalDrawableTopImg;
			pressedDrawable = pressedDrawableTopImg;
		}
		if (normalDrawableLeftImg != null && pressedDrawableLeftImg != null) {
			drawableLocation = DrawableLocation.LEFT;
			normalDrawable = normalDrawableLeftImg;
			pressedDrawable = pressedDrawableLeftImg;
		}
		if (normalDrawableRightImg != null && pressedDrawableRightImg != null) {
			drawableLocation = DrawableLocation.RIGHT;
			normalDrawable = normalDrawableRightImg;
			pressedDrawable = pressedDrawableRightImg;
		}
		tdarr.recycle();
		updateDrawable(normalDrawable);
	}

	/**
	 * 判断该次点击操作是否有效
	 * 
	 * @param event
	 * @return true:有效；false:无效
	 * @time 2013-4-28上午10:57:33
	 */
	public static boolean isClick(MotionEvent event, View view) {
		return event.getX() > 0 && event.getY() > 0 && event.getX() < view.getWidth() && event.getY() < view.getHeight();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setTextColor(textTouchColor);
			updateDrawable(pressedDrawable);
			break;
		case MotionEvent.ACTION_UP:
			setTextColor(textColor);
			updateDrawable(normalDrawable);
			if (isClick(event, this)) {
				performClick();// 如果不加该方法，则在为该按钮添加点击事件时，点击事件失效
			}
			break;
		default:
			break;
		}
		return true;
	}

	public void updateDrawable(Drawable drawable) {
		if (drawable != null) {
			if (drawableLocation == DrawableLocation.LEFT) {
				setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
			} else if (drawableLocation == DrawableLocation.TOP) {
				setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
			} else if (drawableLocation == DrawableLocation.RIGHT) {
				setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
			} else if (drawableLocation == DrawableLocation.BOTTOM) {
				setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
			}
		}
	}

}
